package com.eco.fanliapp.admin.controller.web;

import com.eco.fanliapp.admin.annotation.SysLogAnnotation;
import com.eco.fanliapp.admin.common.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.service.IAppOrderDetailService;
import com.eco.fanliapp.utils.RegUtils;
import com.eco.fanliapp.vo.UserVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/web/order")
public class OrderDetailController extends BaseController {

    @Autowired
    private IAppOrderDetailService orderDetailService ;

    /**
     * 查询订单明细（效率低废弃 20191008 by zhuchao）
     * @return
     */
    @RequestMapping("/selectAppOrderDetailList")
    public ServerResponse<PageInfo> selectAppOrderDetailList(@RequestParam(defaultValue = "1") Integer currentPage , @RequestParam(defaultValue = "10") Integer pageSize,
                                                             String goodsPlatform , String payStatus, String startTime, String endTime, String keyWord ){

        Integer companyId = getAuthentication().getCompanyId();
        return orderDetailService.selectAppOrderDetailList(currentPage,pageSize,companyId,goodsPlatform,payStatus,startTime,endTime,keyWord);
    }

    /**
     * 查询订单明细
     * @param currentPage
     * @param pageSize
     * @param goodsPlatform
     * @param payStatus
     * @param startTime
     * @param endTime
     * @param keyWord
     * @return
     */
    @RequestMapping("/selectAppOrderDetailListNew")
    public ServerResponse<PageInfo> selectAppOrderDetailListNew(@RequestParam(defaultValue = "1") Integer currentPage , @RequestParam(defaultValue = "10") Integer pageSize,
                                                             String goodsPlatform , String payStatus, String startTime, String endTime, String keyWord ,Integer deleted){

        Integer companyId =  getAuthentication().getCompanyId();
        if(companyId == 1){
            companyId = null;
        }

        return orderDetailService.selectAppOrderDetailListNew(currentPage,pageSize,companyId,goodsPlatform,payStatus,startTime,endTime,keyWord,deleted);
    }

    /**
     * 根据关键字查询订单
     * @param payStatus
     * @param startTime
     * @param endTime
     * @param keyWord
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/selectAppOrderDetailByKeyWord")
    public ServerResponse<PageInfo> selectAppOrderDetailByKeyWord(String goodsPlatform , String payStatus, String startTime, String endTime, String keyWord,
                                                                  @RequestParam(defaultValue = "1") int currentPage, @RequestParam(defaultValue = "10") int pageSize){
        Integer companyId = getAuthentication().getCompanyId();
       return orderDetailService.selectAppOrderDetailByKeyWord(goodsPlatform,payStatus,startTime,endTime,keyWord,currentPage,pageSize,companyId);

    }
    @SysLogAnnotation(value = "删除订单",userType = 2)
    @RequestMapping(method = RequestMethod.GET,value = "/delete")
    public ServerResponse delete(String orderDetailid){
        int i = orderDetailService.deleteAppOrderDetailById(orderDetailid);
        if(i > 0){
         return    ServerResponse.createBySuccessMessage("删除成功");
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }

    /**
     * 设置违规订单
     * @return
     */
    @GetMapping("/violations/toSet")
    public  ServerResponse  violationsToSet(Long tradeId){
        UserVO authentication = getAuthentication();
        return orderDetailService.violationsToSet(tradeId,authentication);
    }

    /**
     * 违规订单
     * @return
     */
    @RequestMapping("/violations/import")
    public  ServerResponse  violationsToSetList(HttpServletResponse response, MultipartFile file){
//        UserVO authentication = getAuthentication();
//        return ServerResponse.createBySuccess("导入成功",orderDetailService.violationsToSetList(response,file, authentication));

        UserVO authentication = getAuthentication();
        return ServerResponse.createBySuccess("导入成功",orderDetailService.maintainOrderList(response,file, authentication));
    }

    /**
     * 订单删除接口
     * @return
     */
    @GetMapping("/deleteOrderDetail")
    public  ServerResponse  deleteOrderDetail(Long orderId){
        int i = orderDetailService.deleteOrderDetail(orderId);
        if(i > 0){
            return ServerResponse.createBySuccess(i);
        }
        return ServerResponse.createByError();
    }

    /**
     * 批量处理跑路商家订单处理
     * @return
     */
    @GetMapping("/handleDetailStatus")
    public  ServerResponse  handleDetailStatus(){
        boolean bool = orderDetailService.handleDetailStatus();
        return ServerResponse.createBySuccessMessage("处理结果："  + bool);
    }

    /**
     * 维权订单(还用以前的接口/violations/import)
     * @return
     */
//    @RequestMapping("/maintainOrderList/import")
//    public  ServerResponse  maintainOrderList(HttpServletResponse response, MultipartFile file){
//        UserVO authentication = getAuthentication();
//        return ServerResponse.createBySuccess("导入成功",orderDetailService.maintainOrderList(response,file, authentication));
//    }

}
